package com.jojo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.DoubleValue;
import com.jojo.domain.JsonVO;
import com.jojo.domain.ResultStatus;
import com.jojo.entity.charts.*;
import com.jojo.service.ChartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "可视化图表接口")
@RestController
@RequestMapping(value = "/charts")
public class ChartController {

    //将service层组件注入
    @Autowired
    ChartService chartService;
    //该注解用于生成接口接口文档，标注是一个接口方法
    @ApiOperation(value = "图表1：职业与平均睡眠持续时间条形图")
    //@GetMapping("/chart1") = @RequestMapping(value = "/chart1", method = RequestMethod.GET)
    //该注解表示此接口只能接收GET请求。
    @GetMapping("/chart1")
    public JSONObject getChart1(){
        List<Chart1> list = chartService.getChart1();
        //将结果集合转化为对应数组
        String[] occupation = list.stream().map(Chart1::getOccupation).toArray(String[]::new);
        Double[] avg_sleep_duration = list.stream().map(Chart1::getAvgSleepDuration).toArray(Double[]::new);
        JSONObject object = new JSONObject();
        object.put("occupation",occupation);
        object.put("avg_sleep_duration",avg_sleep_duration);
        return object;
    }

    @ApiOperation(value = "图表2：人员BMI分类饼图")
    @GetMapping("/chart2")
    public JSONArray getChart2(){
        List<Chart2> list = chartService.getChart2();
        //将结果集合转化为对应数组
        JSONArray array = new JSONArray();
        array.addAll(list);
        return array;
    }

    @ApiOperation(value = "图表3：年龄与睡眠质量折线图")
    @GetMapping("/chart3")
    public JSONObject getChart3(){
        List<Chart3> list = chartService.getChart3();
        //将结果集合转化为对应数组
        String[] age = list.stream().map(Chart3::getAge).toArray(String[]::new);
        Double[] quality = list.stream().map(Chart3::getQuality).toArray(Double[]::new);
        JSONObject object = new JSONObject();
        object.put("age",age);
        object.put("quality",quality);
        return object;
    }

    @ApiOperation(value = "图表4：每日步数与睡眠持续时间散点图")
    @GetMapping("/chart4")
    public JSONObject getChart4(){
        List<Chart4> list = chartService.getChart4();
        //将结果集合转化为对应数组
        String[] steps = list.stream().map(Chart4::getSteps).toArray(String[]::new);
        Double[] sleep_duration = list.stream().map(Chart4::getSleepDuration).toArray(Double[]::new);
        JSONObject object = new JSONObject();
        object.put("steps",steps);
        object.put("sleep_duration",sleep_duration);
        return object;
    }

    @ApiOperation(value = "图表5：各年龄段睡眠情况雷达图")
    @GetMapping("/chart5")
    public JsonVO<JSONArray> getChart5(){
        String message = "avgHeartRate," + "avgQualityOfSleep,"
                + "ageGroup," + "avgPhysicalActivityLevel," + "avgStressLevel," + "avgSleepDuration,";
        return JsonVO.create(chartService.getChart5(),200,message);
    }

    @ApiOperation(value = "图表6：睡眠障碍分布情况矩形树图")
    @GetMapping("/chart6")
    public JSONArray getChart6(){
        List<Chart6> list = chartService.getChart6();
        JSONArray array = new JSONArray();
        array.addAll(list);
        return array;
    }

}
